package com.platform.modules.acme.component;


import com.platform.config.AcmeProperties;
import com.platform.modules.acme.utils.AcmeUtil;
import org.shredzone.acme4j.Account;
import org.shredzone.acme4j.AccountBuilder;
import org.shredzone.acme4j.Login;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.exception.AcmeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ConnectException;
import java.security.KeyPair;

@Component
public class LetsEncrypt {


    @Autowired
    AcmeProperties acme;

    Login login = null;

    /**
     * Create an Account or use an Account to connection server
     *
     * @return
     */
    public Login createLogin() {
        //先创建连接session
        try {
            Session session = new Session(acme.getLetsEncryptServerPath());
            KeyPair keyPair = AcmeUtil.loadOrCreateUserKeyPair(acme.getLetsEncryptKeyPair());
            if (login == null){
                login = new AccountBuilder().agreeToTermsOfService().useKeyPair(keyPair).createLogin(session);
            }
            return login;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
